.TH std::experimental::optional::value 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::experimental::optional::value \- std::experimental::optional::value

.SH Synopsis
   constexpr T& value() &;               \fB(1)\fP (library fundamentals TS)
   constexpr const T & value() const &;
   constexpr T&& value() &&;             \fB(2)\fP (library fundamentals TS)
   constexpr const T&& value() const &&;

   Returns the contained value.

   1) Equivalent to return bool(*this) ? *val : throw bad_optional_access();.
   2) Equivalent to return bool(*this) ? std::move(*val) : throw
   bad_optional_access();.

.SH Parameters

   \fI(none)\fP

.SH Return value

   A reference to the contained value.

.SH Exceptions

   std::experimental::bad_optional_access if *this does not contain a value.

.SH Notes

   The dereference operator operator*() does not check if this optional contains a
   value, which may be more efficient than value().

.SH Example


// Run this code

 #include <experimental/optional>
 #include <iostream>

 int main()
 {
     std::experimental::optional<int> opt = {};

     try
     {
         int n = opt.value();
     }
     catch (const std::logic_error& e)
     {
         std::cout << e.what() << '\\n';
     }
 }

.SH Possible output:

 optional<T>::value: not engaged

.SH See also

                             returns the contained value if available, another value
   value_or                  otherwise
                             \fI(public member function)\fP
   operator->                accesses the contained value
   operator*                 \fI(public member function)\fP
   bad_optional_access       exception indicating checked access to an optional that
   (library fundamentals TS) doesn't contain a value
                             \fI(class)\fP

.SH Category:
     * Noindexed pages
